package com.xiaohong.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.file.feign.FileFeign;
import com.xiaohong.user.pojo.Friend;
import com.xiaohong.user.pojo.User;
import com.xiaohong.user.pojo.UsernameList;
import com.xiaohong.user.service.ChatFriendService;
import com.xiaohong.user.service.ChatUserService;
import com.xiaohong.user.service.ChatUsernameListService;
import com.xiaohong.websocket.feign.ChatCentreFeigh;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Tag(name = "ChatUser接口文档")
@RestController
@RequestMapping("/chatUser")
@CrossOrigin
public class ChatUserController {

    @Autowired
    private ChatUserService chatUserService;

    @Autowired
    private ChatFriendController chatFriendController;

    @Autowired
    private ChatUsernameListService chatUsernameListService;

    @Autowired
    private ChatCentreFeigh chatCentreFeigh;

    @Autowired
    private FileFeign fileFeign;

    @RequestMapping("/test")
    public Result test(){
        return fileFeign.getFileInfo("http://119.29.67.157:8080/xiaohong/M00/00/00/CgAICGCkXu2AUhwfAABsVujZrek065.jpg");
    }

    @Operation(summary = "用户注册",description = "所需权限：")
    @PostMapping("/register")
    public Result register(@RequestBody  @Parameter(name = "ChatUser对象",required = true) User user){
        // 分配py号
        UsernameList usernameList = new UsernameList();
        usernameList.setIsAble("true");
        List<UsernameList> list = chatUsernameListService.findList(usernameList);
        if (list!= null && list.size() > 0){
            UsernameList ul = list.get(0);
            user.setUsername(ul.getId());
            ul.setIsAble("false");
            chatUsernameListService.update(ul);
            // 分配了一个在增加
            UsernameList iul = new UsernameList();
            iul.setIsAble("true");
            chatUsernameListService.add(iul);
            //对用户密码进行加盐加密存入数据库
            user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
            //调用ChatUserService实现添加ChatUser
            chatUserService.add(user);
            return new Result(true,StatusCode.OK,"注册成功",user);
        }else{
            return new Result(true,StatusCode.ERROR,"注册失败");
        }

    }

    /***
     * 分页条件搜索实现ChatUser
     * @param user
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询ChatUser",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "ChatUser对象",required = true) User user, @PathVariable int page, @PathVariable  int size){
        //调用ChatUserService实现分页条件查询ChatUser
        Page pageInfo = chatUserService.findPage(user, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现ChatUser
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询ChatUser",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用ChatUserService实现分页查询ChatUser
        Page pageInfo = chatUserService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索ChatUser数据
     * @param user
     * @return
     */
    @Operation(summary = "多条件查询ChatUser",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<User>> findList(@RequestBody(required = false) @Parameter(name = "ChatUser对象",required = true) User user){
        //调用ChatUserService实现条件查询ChatUser
        List<User> list = chatUserService.findList(user);
        return new Result<List<User>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除chatUser数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除ChatUser",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用ChatUserService实现根据ID删除
        chatUserService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改ChatUser数据
     * @param user
     * @return
     */
    @Operation(summary = "根据ID修改ChatUser",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result update(@RequestBody @Parameter(name = "ChatUser对象",required = true) User user){
        //调用ChatUserService实现修改ChatUser
        chatUserService.update(user);
        return new Result(true,StatusCode.OK,"修改成功",user);
    }

    /***
     * 新增ChatUser数据
     * @param user
     * @return
     */
    @Operation(summary = "添加ChatUser",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "ChatUser对象",required = true) User user){
        //调用ChatUserService实现添加ChatUser
        chatUserService.add(user);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询ChatUser数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询ChatUser",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    @GetMapping("/{id}")
    public Result<User> findById(@PathVariable Long id){
        //调用ChatUserService实现根据ID查询ChatUser
        User user = chatUserService.findById(id);
        user.setStatus(chatCentreFeigh.getStatus(id).getData());
        return new Result<User>(true,StatusCode.OK,"查询成功", user);
    }

    /***
     * 查询ChatUser全部数据
     * @return
     */
    @Operation(summary = "查询所有ChatUser",description = "所需权限：admin",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result<List<User>> findAll(){
        //调用ChatUserService实现查询所有ChatUser
        List<User> list = chatUserService.findAll();
        return new Result<List<User>>(true, StatusCode.OK,"查询成功",list) ;
    }

    /***
     * 条件搜索
     * key  关键字
     * @return
     */
    @Operation(summary = "条件搜索",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping("/search")
    public Result SearchUser(
            @RequestParam @Parameter(name = "key",required = true) String key,
            @RequestParam @Parameter(name = "uid",required = true) Long uid
    ){
        //调用ChatUserService实现查询所有ChatUser
        List<User> list = chatUserService.SearchUser(key);
        // 判断是不是好友
        for (User user : list) {
            if (user.getUsername().equals(uid)){
                user.setAdded(true);
            }else{
                Friend friend = new Friend();
                friend.setUserId(uid);
                friend.setFriendId(user.getId());
                user.setAdded(chatFriendController.isFriend(friend).getData());
            }
        }
        return new Result(true, StatusCode.OK,"查询成功",list) ;
    }

}
